package com.yang.tools.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

public class HttpUtil
{

	private Properties prop;

	private HttpURLConnection conn = null;
	private final String protocol = "http://";
	private String urlLink;
	private String method;
	private String encoding;
	private int timeout = -1;

	public HttpUtil()
	{
		method = "GET";
		encoding = "utf-8";
		timeout = 3000;
	}

	public HttpUtil(String urlLink)
	{
		this();
		this.urlLink = urlLink;
	}

	public HttpUtil(String urlLink, String proxyHost, String proxyPort)
	{
		this(urlLink);
		prop = System.getProperties();
		prop.setProperty("http.proxyHost", proxyHost);
		prop.setProperty("http.proxyPort", proxyPort);
	}

	private void connect() throws Exception
	{

		try
		{
			URL url = new URL(urlLink);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod(method);
			conn.setRequestProperty("Accept-Charset", encoding);
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setConnectTimeout(timeout);

			if (conn.getResponseCode() != HttpURLConnection.HTTP_OK)
			{
				throw new Exception("could not connect to " + urlLink);
			}

		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	private void connect(String urlLink) throws Exception
	{
		this.urlLink = urlLink;
		connect();
	}

	private void disconnect() throws Exception
	{
		if (conn != null)
		{
			conn.disconnect();
		}
	}

	public void download(String srcurlfile, String destFolder) throws Exception
	{
		urlLink = srcurlfile;
		download(destFolder);
	}

	public void download(String destFolder) throws Exception
	{
		connect();

		File dir = new File(destFolder);
		destFolder = PathUtil.completeTheTailSlash(destFolder);
		urlLink = PathUtil.completeTheTailSlash(urlLink);

		File file = new File(destFolder + PathUtil.getLastFolderName(urlLink));
		if (dir.exists() == false)
		{
			dir.mkdirs();
		}
		InputStream inStream = conn.getInputStream();
		FileOutputStream writer = new FileOutputStream(file);
		byte[] buffer = new byte[1024];
		int length;
		while ((length = inStream.read(buffer)) > 0)
		{
			writer.write(buffer, 0, length);
		}

		disconnect();
	}

	public String getResponseSource(String srcurlfile) throws Exception
	{
		urlLink = srcurlfile;
		return getResponseSource();
	}

	public String getResponseSource() throws Exception
	{
		connect();

		StringBuffer source = new StringBuffer();
		InputStream responseStream;

		try
		{
			responseStream = conn.getInputStream();
			byte[] buf = new byte[40960];
			int length;

			while ((length = responseStream.read(buf)) > -1)
			{
				source.append(new String(buf, 0, length, "UTF-8"));
			}

		}
		catch (IOException e)
		{
			throw new Exception(e.getMessage());
		}
		finally
		{
			disconnect();
		}
		return source.toString();
	}

}
